﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_RoleDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_Role(T_Role en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///同一个PID里的不能有名字重复的模块
                if (conn.T_Role.Select(c => c).Where(c => c.ROLE_NAME == en.ROLE_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_Role.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_Role(T_Role en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///不能有名字重复的,并且ID号不相同
                if (conn.T_Role.Select(c => c).Where(c => c.ROLE_NAME == en.ROLE_NAME && c.ROLE_ID != en.ROLE_ID).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_Role.Select(c => c).Where(c => c.ROLE_ID == en.ROLE_ID);
                    foreach (var n in query)
                    {
                        if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "ROLE_ID"))
                            return _ReturnValue.Err;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }
        /// <summary>
        /// 获取角色，如果需要视图，则去视图DAL里获取
        /// </summary>
        /// <param name="status">角色是否启用的，-1 代表全部，0. 代表不启动， 1. 代表启动的</param>
        /// <param name="name">角色的名称 空或NULL代表不忽略</param>
        /// <param name="start">分页的起始行，-1 代表不分页</param>
        /// <param name="row">每页显示数， -1 代表不分页</param>
        /// <param name="count">总数</param>
        /// <returns></returns>
        public static List<T_Role> Get_Role(out int count,int status=1, string name="", int start=-1, int row=-1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_Role.Select(c => c);
                if (status > -1)
                    query = query.Where(c => c.ROLE_STATUS == status);
                if (name != null && name != "")
                    query = query.Where(c => c.ROLE_NAME.Contains(name));
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c=>c.ROLE_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch(Exception ex) { return null; }
        }


        public static T_Role Get_Role(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_Role.Select(c => c).Where(c => c.ROLE_ID == id).Single();
            }
            catch { return null; }
        }
    }
}
